<script type="text/javascript" src="/static/bootstrap/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/static/layer/layer.js"></script>
<script type="text/javascript" src="/static/artDialog/dist/dialog-min.js"></script>
<script th:inline="javascript">
    var fzqblog = {};
    fzqblog.realpath = "";
    fzqblog.curUrl = window.location.href;
    fzqblog.userId = [[${user?.userId}]];
    fzqblog.topUrl = {
        loadMessage: "/user/msg/list",
        loadMessageCount: "/user/msg/count"
    };


    if (fzqblog.userId != "0") {
        getMessageCount();
    }

    //获取消息数量
    function getMessageCount() {

        $.ajax({
            url: fzqblog.topUrl.loadMessageCount,
            type: 'get',
            dataType: 'json',
            data: {status: 0},
            success: function (response) {
                var count = response.data;
                if (count > 0) {
                    $("#message-count").text(count);
                    setInterval(function () {
                        if ($("#message-count").hasClass("shake")) {
                            $("#message-count").removeClass("shake");
                        } else {
                            $("#message-count").addClass("shake");
                        }
                    }, 1000);
                }
            }
        });
    }

    //显示消息框
    function showMessageList() {
        $("#message-dialog").show();
        if ($("#message-data-list").children().length == 0 && !fzqblog.isLoading) {
            loadMessage();
        }
    }


    function MessageItem(data) {
        var item = $("<div class='message-item'></div>");
        $("<div>" + data.description + "</div>").appendTo(item);
        var op = $("<div class='message-op'></div>").appendTo(item);
        $("<span class='time'>发送：" + data.createTime + "</span>").appendTo(op);
        if (data.url != "") {
            $('<a href="/read/msg?id=' + data.id + '" class="message-detail" class="read-message" url="' + data.url + '" messageId="' + data.id + '">查看详情</a>').appendTo(op);
        }
        return item;
    }

    function loadMessage(page) {
        fzqblog.isLoading = true;
        fzqblog.messagePageNo = page;
        $("#load-more-message").remove();
        $('<div id="loading-message" class="loading"> <div class="loading-con"><img src="/static/images/loading.gif"/><span>正在加载.......</span></div></div>').appendTo($("#message-load"));

        $.ajax({
            url: fzqblog.topUrl.loadMessage,
            type: 'get',
            dataType: 'json',
            data: {status: 0},
            success: function (res) {
                fzqblog.isLoading = false;
                $("#loading-message").remove();
                var list = res.data.list;
                var simplePage = res.data.page;
                if (simplePage.count == 0) {
                    $("<div class='no-data'>没有消息</div>").appendTo($("#message-data-list"));
                } else {
                    for (var i = 0, _len = list.length, data; i < _len, data = list[i]; i++) {
                        new MessageItem(data).appendTo($("#message-data-list"));
                    }
                    if (simplePage.pageTotal > simplePage.page) {
                        $('<div id="load-more-message" class="load-more"><a href="javascript:;">⇓加载更多</a></div>').appendTo($("#message-load"));
                    }
                }
            }
        });
    }

    $(document).ready(function () {
        $("#searchBtn").click(function () {
            var keyword = $.trim($("#keyWord").val());
            if (keyword == "") {
                layer.msg("搜索内容不能为空", {
                    icon: 5,
                    time: 1500 //2秒关闭（如果不配置，默认是3秒）
                });
                return;
            }
            if (keyword.lenth > 20) {
                layer.msg("搜索内容不能超过20个字符", {
                    icon: 5,
                    time: 1500 //2秒关闭（如果不配置，默认是3秒）
                });
                return;
            }
            if (fzqblog.curUrl.indexOf("search") == -1) {
                searchForm.submit();
            } else {
                searchData();
            }
        });

    });

</script>

<script type="text/javascript" src="/static/js/fzqblog.js"></script>
